package com.coocaa.app.gamecenter.pages.arsenal.view;

import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.coocaa.app.gamecenter.pages.arsenal.data.WeaponData;
import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.R;

/**
 * Created by xfk on 2015/11/5.
 */
public class ArsenalItemView extends RelativeLayout {
    private static String tag = "arsenal";
    private Context mContext;
    private TextView textView;
    private WeaponClassView weaponListView;

    public ArsenalItemView(Context context) {
        super(context);
        mContext = context;
        createContentView();
    }

    public void setData(WeaponData weaponData) {
        textView.setText(weaponData.name);
        weaponListView.setData(weaponData.weaponList);
        weaponListView.setArsenalItemId(this.getId());
    }

    private void createContentView() {
        LinearLayout linearLayout = new LinearLayout(mContext);
        LayoutParams linearParams = new LayoutParams(CoocaaApplication.Div(535), CoocaaApplication.Div(137));
        linearParams.topMargin = CoocaaApplication.Div(0);
        linearParams.leftMargin = CoocaaApplication.Div(40);
        addView(linearLayout, linearParams);

        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        ImageView imageView1 = new ImageView(mContext);
        imageView1.setImageResource(R.drawable.arsenal_title_line);
        LinearLayout.LayoutParams imaParams = new LinearLayout.LayoutParams(CoocaaApplication.Div(160), ViewGroup.LayoutParams.WRAP_CONTENT);
        imaParams.gravity = Gravity.CENTER;
        linearLayout.addView(imageView1, imaParams);

        textView = new TextView(mContext);
        textView.setTextSize(CoocaaApplication.Dpi(38));
        textView.setText("军火库");
        textView.setGravity(Gravity.CENTER);
        LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(CoocaaApplication.Div(215), ViewGroup.LayoutParams.WRAP_CONTENT);
        textParams.gravity = Gravity.CENTER;
        linearLayout.addView(textView, textParams);

        ImageView imageView2 = new ImageView(mContext);
        imageView2.setImageResource(R.drawable.arsenal_title_line);
        LinearLayout.LayoutParams img2Params = new LinearLayout.LayoutParams(CoocaaApplication.Div(160), ViewGroup.LayoutParams.WRAP_CONTENT);
        img2Params.gravity = Gravity.CENTER;
        linearLayout.addView(imageView2, img2Params);

        weaponListView = new WeaponClassView(mContext);
        LayoutParams listParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, CoocaaApplication.Div(343));
        listParams.topMargin =CoocaaApplication.Div(120);
        addView(weaponListView, listParams);
    }

    public void requestItemFocus(int index) {
        if(weaponListView != null)
            weaponListView.requestItemFocus(index);
    }

    public void requestItemFocusByPos(int pos) {
        if(weaponListView != null)
            weaponListView.requestItemFocusByPos(pos);
    }

    public View getItemView(int index) {
        View view = null;
        if(weaponListView != null)
            view = weaponListView.getItemView(index);
        return view;
    }

    public void setItemViewFocusable(boolean b) {
        if(weaponListView != null) {
            weaponListView.setItemViewFocusable(b);
        }
    }

    public void setFocusChangedEvent(ItemFocusView.FocusChangedEvent focusChangedEvent) {
        if(weaponListView != null)
            weaponListView.setFocusChangedEvent(focusChangedEvent);
    }

	public void setItemEventChangeInterface(ItemListView.ItemEventChangeInterface changeInterface) {
        if(weaponListView != null)
            weaponListView.setItemEventChangeInterface(changeInterface);
    }

    public void setSlideUpAndDownListener(ItemListView.SlideUpAndDownListener listener) {
        if(weaponListView != null)
            weaponListView.setSlideUpAndDownListener(listener);
    }

    public void onDestroy() {
        if(weaponListView != null) {
            weaponListView.onDestroy();
            weaponListView.destroyDrawingCache();
            removeView(weaponListView);
            weaponListView = null;
        }

        if(textView != null) {
            textView.destroyDrawingCache();
            textView = null;
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        LogUtils.i(tag, "onkeydown= " + keyCode);
        return super.onKeyDown(keyCode, event);
    }
}
